package com.tianxin.workbao.workBao.utils.common;

import android.content.Context;
import android.os.Handler;
import android.widget.Toast;

import com.tianxin.workbao.workBao.R;

import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * Created by hacker on 2017/6/19.
 */

public class AlertUtils {
    private Context mContext;
    private Handler mHandler;
    private SweetAlertDialog mDialog;

    public AlertUtils(Context context) {
        this.mContext = context;
        mHandler = new Handler();
    }

    public void showBasicMessage(String titleText) {
        new SweetAlertDialog(mContext).setTitleText(titleText).show();
    }

    public void showError(String titleText, String content) {
        mDialog = new SweetAlertDialog(mContext).setTitleText(titleText).setContentText(content);
        mDialog.show();
    }

    public void showTitleUnder(String titleText, String content) {
        new SweetAlertDialog(mContext).setTitleText(titleText).setContentText(content).show();
    }

    public void showWarning(String titleText, String content, String confirmText) {
        mDialog = new SweetAlertDialog(mContext, SweetAlertDialog.WARNING_TYPE)
                .setTitleText(titleText)
                .setContentText(content)
                .setConfirmText(confirmText);
        mDialog.show();
    }

    public void showSuccess(String titleText, String content) {
        mDialog = new SweetAlertDialog(mContext, SweetAlertDialog.SUCCESS_TYPE)
                .setTitleText(titleText)
                .setContentText(content);
        mDialog.show();
    }

    public void showCustmIcon(String titleText, String content, int image) {
        mDialog = new SweetAlertDialog(mContext, SweetAlertDialog.CUSTOM_IMAGE_TYPE)
                .setTitleText(titleText)
                .setContentText(content)
                .setCustomImage(image);
        mDialog.show();
    }

    public void showConfirming(String titleText, String content, String confirmText, SweetAlertDialog.OnSweetClickListener listener) {
        mDialog = new SweetAlertDialog(mContext, SweetAlertDialog.WARNING_TYPE)
                .setTitleText(titleText)
                .setContentText(content)
                .setConfirmText(confirmText)
                .setCancelText("否")
                .setConfirmClickListener(listener)
                .setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        mDialog.dismiss();
                    }
                })
        ;

        mDialog.show();
    }

    public void showCancelBind(final String titleText, String content, final String confirmText, String cancelText) {
        mDialog = new SweetAlertDialog(mContext, SweetAlertDialog.WARNING_TYPE)
                .setTitleText(titleText)
                .setContentText(content)
                .setCancelText(cancelText)
                .setConfirmText(confirmText)
                .showCancelButton(true)
                .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        Toast.makeText(mContext, confirmText, Toast.LENGTH_SHORT).show();
                    }
                })
                .setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sDialog) {
                        sDialog.cancel();
                    }
                });
        mDialog.show();
    }

    public void showPupConfirming(String titleText, String content, String confirmText) {
        mDialog = new SweetAlertDialog(mContext, SweetAlertDialog.WARNING_TYPE)
                .setTitleText(titleText)
                .setContentText(content)
                .setConfirmText(confirmText)
                .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(final SweetAlertDialog pDialog) {
                        pDialog.changeAlertType(SweetAlertDialog.PROGRESS_TYPE);
                        pDialog.getProgressHelper().setBarColor(mContext.getResources().getColor(R.color.alertDialog_Progress_Color));
                        pDialog.getProgressHelper().setBarWidth(5);
                        pDialog.getProgressHelper().setCircleRadius(100);
                        pDialog.getProgressHelper().setRimColor(mContext.getResources().getColor(R.color.alertDialog_Progress_hintColor));
                        pDialog.getProgressHelper().setRimWidth(5);
                        pDialog.setTitleText("Please later...");
                        pDialog.setContentText("");
                        pDialog.setCancelable(false);
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                pDialog.setTitleText("Deleted!")
                                        .setContentText("Your imaginary file has been deleted!")
                                        .setConfirmText("OK")
                                        .setConfirmClickListener(null)
                                        .changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
                                pDialog.show();
                            }
                        }, 3000);
                    }
                });
        mDialog.show();
    }

    public void showDialog(String loadingText, int autoCloseTime) {
        if (mDialog == null) {
            mDialog = new SweetAlertDialog(mContext, SweetAlertDialog.PROGRESS_TYPE);
        }
        mDialog.changeAlertType(SweetAlertDialog.PROGRESS_TYPE);
        mDialog.getProgressHelper().setBarColor(mContext.getResources().getColor(R.color.colorPrimary));
        mDialog.getProgressHelper().setBarWidth(5);
        mDialog.getProgressHelper().setCircleRadius(100);
        mDialog.getProgressHelper().setRimColor(mContext.getResources().getColor(R.color.alertDialog_Progress_hintColor));
        mDialog.getProgressHelper().setRimWidth(5);
        mDialog.setTitleText(loadingText);
        mDialog.setCancelable(false);
        try {
            mDialog.show();
            if (autoCloseTime > 0) {
                postDelayedDismiss(autoCloseTime);
            }
        } catch (Exception e) {

        }

    }

    public void cancelPre() {
        if (mDialog != null) {
            mDialog.cancel();
        }
    }

    public void dismiss() {
        if (mDialog != null) {
            mDialog.dismiss();
        }
    }

    public void postDelayedDismiss(int time) {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (mDialog != null) {
                    try {
                        mDialog.dismiss();
                    } catch (Exception e) {

                    }
                }
            }
        }, time);
    }
}
